<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>消息管理</el-breadcrumb-item>
      <el-breadcrumb-item>告警设置</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-table :data="alarmList" stripe style="width: 100%">
        <el-table-column prop="title" label="标题" width="120"></el-table-column>
        <el-table-column prop="content" label="内容" width="220"></el-table-column>
        <el-table-column prop="grade" label="告警级别" width="120"></el-table-column>
        <el-table-column prop="person" label="发送人" width="120"></el-table-column>
        <el-table-column prop="phone" label="电话" width="180"></el-table-column>
        <el-table-column prop="remark" label="备注" width="180"></el-table-column>
        <el-table-column prop="count" label="发送次数" width="80"></el-table-column>
        <el-table-column fixed="right" label="操作" width="180">
          <template slot-scope="scope">
            <el-button @click="openEditAlarm(scope.row)" type="primary" icon="el-icon-edit" size="small">编辑</el-button>
            <el-button type="danger" size="small" icon="el-icon-delete" @click="delRoleById(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog title="编辑紧急联系人" :visible.sync="editDialogVisible" width="50%" @close="closeEdit">
      <el-form :model="editForm" :rules="rulesEdit" ref="editForm" label-width="100px">
        <el-form-item label="姓名" prop="person">
          <el-input v-model="editForm.person"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="editForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="告警级别" prop="grade">
          <el-input v-model="editForm.grade"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="editDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="editAlarm">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Alarm',
  mounted () {
    this.getAlarmList()
  },
  methods: {
    editAlarm () {

    },
    closeEdit () {
      this.$refs.editForm.resetFields()
    },
    openEditAlarm (alarmInfo) {
      this.editForm = alarmInfo
      this.editDialogVisible = true
    },
    async getAlarmList () {
      const { data: res } = await this.$http.get('news/alarm')
      if (res.code !== 200) {
        return this.$message.error(res.msg)
      }
      this.alarmList = res.data
    }
  },
  data () {
    // 验证手机号的规则
    var checkMobile = (rule, value, cb) => {
      // 验证手机号的正则表达式
      const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/

      if (regMobile.test(value)) {
        return cb()
      }

      cb(new Error('请输入合法的手机号'))
    }
    return {
      editDialogVisible: false,
      alarmList: [],
      editForm: {},
      rulesEdit: {
        phone: [
          { required: true, message: '请输入电话', trigger: 'blur' },
          { validator: checkMobile, trigger: 'blur' }
        ]
      }
    }
  }
}
</script>

<style scoped>

</style>
